package org.dromara.system.service.impl;

import org.dromara.common.core.constant.SystemConstants;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.PersonRecordBo;
import org.dromara.system.domain.vo.PersonRecordVo;
import org.dromara.system.domain.PersonRecord;
import org.dromara.system.mapper.PersonRecordMapper;
import org.dromara.system.service.IPersonRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 人员记录Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PersonRecordServiceImpl implements IPersonRecordService {

    private final PersonRecordMapper baseMapper;

    /**
     * 查询人员记录
     *
     * @param id 主键
     * @return 人员记录
     */
    @Override
    public PersonRecordVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询人员记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 人员记录分页列表
     */
    @Override
    public TableDataInfo<PersonRecordVo> queryPageList(PersonRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PersonRecord> lqw = buildQueryWrapper(bo);
        Page<PersonRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的人员记录列表
     *
     * @param bo 查询条件
     * @return 人员记录列表
     */
    @Override
    public List<PersonRecordVo> queryList(PersonRecordBo bo) {
        LambdaQueryWrapper<PersonRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PersonRecord> buildQueryWrapper(PersonRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PersonRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(PersonRecord::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getXm()), PersonRecord::getXm, bo.getXm());
        lqw.eq(bo.getXb() != null, PersonRecord::getXb, bo.getXb());
        lqw.eq(StringUtils.isNotBlank(bo.getNl()), PersonRecord::getNl, bo.getNl());
        lqw.eq(StringUtils.isNotBlank(bo.getSjh()), PersonRecord::getSjh, bo.getSjh());
        lqw.eq(StringUtils.isNotBlank(bo.getGw()), PersonRecord::getGw, bo.getGw());
        lqw.eq(StringUtils.isNotBlank(bo.getSfzhm()), PersonRecord::getSfzhm, bo.getSfzhm());
        lqw.eq(bo.getRylx() != null, PersonRecord::getRylx, bo.getRylx());
        lqw.eq(bo.getCsrq() != null, PersonRecord::getCsrq, bo.getCsrq());
        lqw.eq(PersonRecord::getDelFlag, 0);
        return lqw;
    }

    /**
     * 新增人员记录
     *
     * @param bo 人员记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PersonRecordBo bo) {
        PersonRecord add = MapstructUtils.convert(bo, PersonRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改人员记录
     *
     * @param bo 人员记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PersonRecordBo bo) {
        PersonRecord update = MapstructUtils.convert(bo, PersonRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PersonRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除人员记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public Boolean checkPersonRecordExists(Long personId) {
        return baseMapper.exists(new LambdaQueryWrapper<PersonRecord>().eq(PersonRecord::getId, personId).eq(PersonRecord::getDelFlag, SystemConstants.NORMAL));
    }
}
